package com.example.contactfragment;

import java.util.ArrayList;
import java.util.List;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.boyuan.aboutcase.CaseViewPagerActivity;
import com.boyuan.aboutcase.Caselist;
import com.boyuan.aboutcase.MyGridView;
import com.boyuan.aboutcase.MySwipeMenuListView;
import com.boyuan.aboutcase.NewCaseDetailActivity;
import com.boyuan.huijiaoyu.R;
import com.handmark.pulltorefresh.PullToRefreshBase;
import com.handmark.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.PullToRefreshScrollView;
import com.magicsoft.app.entity.CaseDetailEntity;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.CaseService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;
import com.magicsoft.weitown.fragment.HomeNewFragment.GetRefreshData;
import com.magicsoft.weitown.fragment.HomeNewFragment.MessageReceiver;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class HomeContactMyCaseGroupActivity extends Activity implements OnClickListener{
	
	public static final String MESSAGE_RECEIVED_ACTION = "com.magicsoft.weitown.fragment.MESSAGE_RECEIVED_ACTION";
	private LinearLayout lin_back;
	ArrayList<CaseDetailEntity> uncloselist = new ArrayList<CaseDetailEntity>();
	ArrayList<CaseDetailEntity> closelist = new ArrayList<CaseDetailEntity>();
	ArrayList<CaseDetailEntity> searchuncloselist = new ArrayList<CaseDetailEntity>();
	ArrayList<CaseDetailEntity> searchcloselist = new ArrayList<CaseDetailEntity>();
	MyAdapter adapter;
	String username;
	String mima;
	private boolean progressShow;
	public boolean isrequestok = false;
//	private ListView mListview;
	String id;
	private String title;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private RelativeLayout lay_dangqian;
	private RelativeLayout lay_lishi;
	private TextView tv_dangqian;
	private TextView tv_lishi;
	static int i = 0;
	CaseService caseservice;
	private ChangecaseReceiver changecaseReceiver;
	private ChangecaseReceiver changecaseReceiver2;
	private MySwipeMenuListView mListview1;
	private PullToRefreshScrollView pullToRefreshScrollView;
	private ScrollView scrollView;
	private int pagenum = 1;
	private int pagenum1 = 1;
	private int pagenum2 = 1;
	private int pagenum3 = 1;
	private CaseService caseService;
	private MyGridView mListview2;
	boolean isdianji = true;
	private Button btn_search;
	String keyword;
	private EditText edit_search;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_contact_my_case_group);
		i = 0;
		registChangecase();
		SharedPreferences sp = this.getSharedPreferences("a.xml", 0);
		username = sp.getString("username", "18850198097");
		id = sp.getString("id", "");
		mima = "" + username.substring(5, username.length());
		Intent intent = getIntent();
		title = intent.getStringExtra("title");
		imageLoader = ImageLoader.getInstance();
//		getMyUnclosecasecache();
//		getMyClosecasecache();
		getMyUnclosecase();
		getMyClosecase();
		prepareView();
	}
	
	private void registChangecase(){
		changecaseReceiver2 = new ChangecaseReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.CHANGEMYCASE);
		registerReceiver(changecaseReceiver2, filter);
	}
	
	class ChangecaseReceiver extends BroadcastReceiver{
		
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			getMyUnclosecase();
			getMyClosecase();
		}
	}
	
	@Override
	protected void onDestroy() {
		unregisterReceiver(changecaseReceiver2);
		super.onDestroy();
	}
	
	private void prepareView() {

		lin_back = (LinearLayout) findViewById(R.id.lin_back);
		lin_back.setOnClickListener(this);
		TextView tv_title = (TextView) findViewById(R.id.tv_title_group);
		tv_title.setText(title);
		btn_search = (Button) findViewById(R.id.btn_search);
		btn_search.setOnClickListener(this);
		edit_search = (EditText) findViewById(R.id.edit_search);
		lay_dangqian = (RelativeLayout) findViewById(R.id.lay_dangqian);
		lay_dangqian.setOnClickListener(this);
		lay_lishi = (RelativeLayout) findViewById(R.id.lay_lishi);
		lay_lishi.setOnClickListener(this);
		tv_dangqian = (TextView) findViewById(R.id.tv_dangqian);
		tv_lishi = (TextView) findViewById(R.id.tv_lishi);
//		mListview = (ListView) findViewById(R.id.listView1);
		
		pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview1);
		pullToRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				new GetRefreshData().execute();
			}
		});
		scrollView = pullToRefreshScrollView.getRefreshableView();
		mListview1 = (MySwipeMenuListView) findViewById(R.id.listView_mycase);
		mListview2 = (MyGridView) findViewById(R.id.myGridView1);
		adapter = new MyAdapter();
		mListview1.setAdapter(adapter);
		mListview2.setAdapter(adapter);
		mListview1.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int arg2, long arg3) {
				Intent intent = new Intent(HomeContactMyCaseGroupActivity.this, CaseViewPagerActivity.class);
				if(i == 0){
					String caseid = uncloselist.get(arg2).getCid();
					intent.putExtra("caseid", caseid);
				}else if(i == 1){
					String caseid = closelist.get(arg2).getCid();
					intent.putExtra("caseid", caseid);
				}
				startActivity(intent);
			}
		});
		
		mListview2.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int arg2, long arg3) {
				Intent intent = new Intent(HomeContactMyCaseGroupActivity.this, CaseViewPagerActivity.class);
				if(i == 0){
					String caseid = uncloselist.get(arg2).getCid();
					intent.putExtra("caseid", caseid);
				}else if(i == 1){
					String caseid = closelist.get(arg2).getCid();
					intent.putExtra("caseid", caseid);
				}
				startActivity(intent);
			}
		});
		
		if(i == 0){
			
			SwipeMenuCreator creator = new SwipeMenuCreator() {
				
				@Override
				public void create(SwipeMenu menu) {
					SwipeMenuItem deleteItem = new SwipeMenuItem(HomeContactMyCaseGroupActivity.this);
					deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
							0x3F, 0x25)));
					deleteItem.setWidth(dp2px(70));
					deleteItem.setTitle("关闭");
					deleteItem.setTitleSize(12);
					deleteItem.setTitleColor(Color.WHITE);
					menu.addMenuItem(deleteItem);
				}
			};
			
			mListview1.setMenuCreator(creator);
			mListview1.setOnMenuItemClickListener(new OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
					switch (index) {
					case 0:
						CaseDetailEntity caseDetailEntity = uncloselist.get(position);
						closeCase(caseDetailEntity);
						break;
					}
					return true;
				}
			});
		}
	}
	
	private void closeCase(CaseDetailEntity caseDetailEntity1){
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		String corp_id = sp.getString("accountId", "");
		String realname = sp.getString("realname", "");
		final String title = caseDetailEntity1.getTitle();
		String desc = caseDetailEntity1.getDesc();
		String priority = caseDetailEntity1.getPriority();
		String type_id = caseDetailEntity1.getType_id();
		String cid = caseDetailEntity1.getCid();
		if (caseService == null) {
			caseService = new CaseService(this);
		}

		caseService.ChangeCaseDetail(corp_id, cid, title, 
				desc, type_id, priority, "3", corp_id, realname,
				new PostRecordResponseListener() {

					@Override
					public void onFinish(String succString) {
						getMyUnclosecase();
						getMyClosecase();
						ToastHelper.showMsg(HomeContactMyCaseGroupActivity.this, "关闭case成功", false);
					}

					@Override
					public void onFailed(String failString) {
						ToastHelper.showMsg(HomeContactMyCaseGroupActivity.this,
								failString, false);
					}
				});
	}
	
	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}
	
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			int j = 0;
			if(i == 0){
				j = uncloselist.size();
			}else{
				j = closelist.size();
			}
			return j;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HolderViewStatic viewContent = null;

			if (convertView == null) {

				viewContent = new HolderViewStatic();

				convertView = getLayoutInflater().inflate(R.layout.homemenulist_litem, null);

				viewContent.iv_image = (ImageView) convertView
						.findViewById(R.id.iv_homeltem_image);
				viewContent.tv_title = (TextView) convertView
						.findViewById(R.id.tv_homeltem_title);
				viewContent.tv_content = (TextView) convertView
						.findViewById(R.id.tv_homeltem_content);
				viewContent.tv_time = (TextView) convertView
						.findViewById(R.id.tv_homeltem_time);
				viewContent.tv_homeltem_comefrom = (TextView) convertView
						.findViewById(R.id.tv_homeltem_comefrom);

				convertView.setTag(viewContent);
			} else {
				viewContent = (HolderViewStatic) convertView.getTag();
			}
			if(i == 0 && isdianji == true){
				String ownerName = uncloselist.get(position).getCreater();
				String title = uncloselist.get(position).getTitle();
				String updateTime = uncloselist.get(position).getLast_modified();
				String status = uncloselist.get(position).getStatus();
				if(updateTime != null && (!"".equals(updateTime))){
					String year = updateTime.substring(0, 4);
					String month = updateTime.substring(4, 6);
					String day = updateTime.substring(6, 8);
					String hours = updateTime.substring(8, 10);
					String mins = updateTime.substring(10, 12);
					String second = updateTime.substring(12, 14);
					String time = month + "-" + day + " "+ hours + ":" + mins;
					viewContent.tv_time.setText(time);
				}else{
					viewContent.tv_time.setText("");
				}
//				String comefrom = "case";
//				viewContent.tv_title.setText(title);
//				viewContent.tv_content.setText(ownerName);
				viewContent.tv_content.setText(title);
				viewContent.tv_title.setText(ownerName);
				if("1".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：未处理");
				}else if("2".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：处理中");
				}else if("3".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：已关闭");
				}else if("4".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：等待客户确认");
				}else if("5".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：已完成");
				}
				options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_header)
				.showImageForEmptyUri(R.drawable.default_header)
				.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
				.build();
				String uri = Constant.photourl + uncloselist.get(position).getCreater_id();
				imageLoader.displayImage(uri,viewContent.iv_image);
			}else if(i == 1 && isdianji == true){
				String ownerName = closelist.get(position).getCreater();
				String title = closelist.get(position).getTitle();
				String updateTime = closelist.get(position).getClosed_time();
				if(updateTime != null && (!"".equals(updateTime))){
					String year = updateTime.substring(0, 4);
					String month = updateTime.substring(4, 6);
					String day = updateTime.substring(6, 8);
					String hours = updateTime.substring(8, 10);
					String mins = updateTime.substring(10, 12);
					String second = updateTime.substring(12, 14);
					String time = month + "-" + day + " "+ hours + ":" + mins;
					viewContent.tv_time.setText(time);
					viewContent.tv_time.setText(time);
				}else{
					viewContent.tv_time.setText("");
				}
				String comefrom = "case";
//				viewContent.tv_title.setText(title);
//				viewContent.tv_content.setText(ownerName);
				viewContent.tv_title.setText(ownerName);
				viewContent.tv_content.setText(title);
//				viewContent.tv_homeltem_comefrom.setText("来自："+comefrom);
				viewContent.tv_homeltem_comefrom.setVisibility(View.GONE);
				options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_header)
				.showImageForEmptyUri(R.drawable.default_header)
				.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
				.build();
				String uri = Constant.photourl + closelist.get(position).getCreater_id();
				imageLoader.displayImage(uri,viewContent.iv_image);
			}else if(i == 0 && isdianji == false){
				String ownerName = uncloselist.get(position).getCreater();
				String title = uncloselist.get(position).getTitle();
				String updateTime = uncloselist.get(position).getLast_modified();
				String status = uncloselist.get(position).getStatus();
				if(updateTime != null && (!"".equals(updateTime))){
					String year = updateTime.substring(0, 4);
					String month = updateTime.substring(4, 6);
					String day = updateTime.substring(6, 8);
					String hours = updateTime.substring(8, 10);
					String mins = updateTime.substring(10, 12);
					String second = updateTime.substring(12, 14);
					String time = month + "-" + day + " "+ hours + ":" + mins;
					viewContent.tv_time.setText(time);
				}else{
					viewContent.tv_time.setText("");
				}
//				String comefrom = "case";
//				viewContent.tv_title.setText(title);
//				viewContent.tv_content.setText(ownerName);
				viewContent.tv_content.setText(title);
				viewContent.tv_title.setText(ownerName);
				if("1".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：未处理");
				}else if("2".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：处理中");
				}else if("3".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：已关闭");
				}else if("4".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：等待客户确认");
				}else if("5".equals(status)){
					viewContent.tv_homeltem_comefrom.setText("状态：已完成");
				}
				options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_header)
				.showImageForEmptyUri(R.drawable.default_header)
				.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
				.build();
				String uri = Constant.photourl + uncloselist.get(position).getCreater_id();
				imageLoader.displayImage(uri,viewContent.iv_image);
			}else if(i == 1 && isdianji == false){
				String ownerName = closelist.get(position).getCreater();
				String title = closelist.get(position).getTitle();
				String updateTime = closelist.get(position).getClosed_time();
				if(updateTime != null && (!"".equals(updateTime))){
					String year = updateTime.substring(0, 4);
					String month = updateTime.substring(4, 6);
					String day = updateTime.substring(6, 8);
					String hours = updateTime.substring(8, 10);
					String mins = updateTime.substring(10, 12);
					String second = updateTime.substring(12, 14);
					String time = month + "-" + day + " "+ hours + ":" + mins;
					viewContent.tv_time.setText(time);
					viewContent.tv_time.setText(time);
				}else{
					viewContent.tv_time.setText("");
				}
				String comefrom = "case";
//				viewContent.tv_title.setText(title);
//				viewContent.tv_content.setText(ownerName);
				viewContent.tv_title.setText(ownerName);
				viewContent.tv_content.setText(title);
//				viewContent.tv_homeltem_comefrom.setText("来自："+comefrom);
				viewContent.tv_homeltem_comefrom.setVisibility(View.GONE);
				options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_header)
				.showImageForEmptyUri(R.drawable.default_header)
				.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
				.build();
				String uri = Constant.photourl + closelist.get(position).getCreater_id();
				imageLoader.displayImage(uri,viewContent.iv_image);
			}
			return convertView;
		}
	}
	
	class HolderViewStatic {
		ImageView iv_image;
		TextView tv_title;// 标题
		TextView tv_content;// 内容
		TextView tv_time;// 时间
		TextView tv_homeltem_comefrom;// 来源
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home_contact_my_case_group, menu);
		return true;
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lin_back:
			finish();
			break;
		case R.id.lay_dangqian:
			dangqian();
			break;
		case R.id.lay_lishi:
			lishi();
			break;
		case R.id.btn_search:
			keyword = edit_search.getText().toString().trim();
			isdianji = !isdianji;
			if(isdianji == false){
				btn_search.setText("取消");
				if(i == 0){
					getSearchMyUnclosecase();
					adapter.notifyDataSetChanged();
				}else if(i == 1){
					getSearchMyClosecase();
					adapter.notifyDataSetChanged();
				}
			}else{
				btn_search.setText("搜索");
				if(i == 0){
					getMyUnclosecase();
					adapter.notifyDataSetChanged();
				}else if(i == 1){
					getMyClosecase();
					adapter.notifyDataSetChanged();
				}
			}
			break;
		default:
			break;
		}
	}

	private void lishi() {
		isdianji = true;
		btn_search.setText("搜索");
		i = 1;
		mListview1.setVisibility(View.GONE);
		mListview2.setVisibility(View.VISIBLE);
		adapter.notifyDataSetChanged();
		lay_dangqian.setBackgroundColor(Color.GRAY);
		tv_dangqian.setTextColor(Color.WHITE);
		lay_lishi.setBackgroundColor(Color.WHITE);
		tv_lishi.setTextColor(Color.BLACK);
	}

	private void dangqian() {
		isdianji = true;
		btn_search.setText("搜索");
		i = 0;
		mListview2.setVisibility(View.GONE);
		mListview1.setVisibility(View.VISIBLE);
		adapter.notifyDataSetChanged();
		lay_dangqian.setBackgroundColor(Color.WHITE);
		tv_dangqian.setTextColor(Color.BLACK);
		lay_lishi.setBackgroundColor(Color.GRAY);
		tv_lishi.setTextColor(Color.WHITE);
	}
	
	public void getMyUnclosecasecache(){
		uncloselist.clear();
		ArrayList<CaseDetailEntity> caseList1 = SharePreferenceHelper.getUncloseCaseList(HomeContactMyCaseGroupActivity.this, id);
		if (caseList1.size() > 0) {
			for(int i=0;i<caseList1.size();i++){
				uncloselist.add(caseList1.get(i));
			}
		}else{
		}
	}
	
	public void getMyClosecasecache(){
		closelist.clear();
		ArrayList<CaseDetailEntity> caseList2 = SharePreferenceHelper.getCloseCaseList(HomeContactMyCaseGroupActivity.this, id);
		if (caseList2.size() > 0) {
			for(int i=0;i<caseList2.size();i++){
				closelist.add(caseList2.get(i));
			}
		}else{
		}
	}
	
	public void getMyUnclosecase(){
		if(caseservice == null){
			caseservice = new CaseService(HomeContactMyCaseGroupActivity.this);
		}
		caseservice.getUnclsecase(id, String.valueOf(pagenum), new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				Caselist caselist = (Caselist) params[0];
//				if(caselist != null){
//					List<CaseDetailEntity> case_list = caselist.getCase_list();
//					SharePreferenceHelper.saveUncloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
//					uncloselist.clear();
//					for(int i=0;i<case_list.size();i++){
//						uncloselist.add(case_list.get(i));
//					}
//					adapter.notifyDataSetChanged();
//				}
				if(caselist != null){
					List<CaseDetailEntity> case_list = caselist.getCase_list();
					if(pagenum == 1){
						uncloselist.clear();
					}
					if (case_list != null && case_list.size() == 0) {
						pagenum--;
						ToastHelper.showMsg(HomeContactMyCaseGroupActivity.this, "已加载到最新", false);
						return;
					}
					SharePreferenceHelper.saveUncloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
					for(int i=0;i<case_list.size();i++){
						uncloselist.add(case_list.get(i));
					}
					adapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onFailed(String Message) {
				Toast.makeText(HomeContactMyCaseGroupActivity.this, Message, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public void getSearchMyUnclosecase(){
		if(caseservice == null){
			caseservice = new CaseService(HomeContactMyCaseGroupActivity.this);
		}
		caseservice.getSearchUnclsecase(id, String.valueOf(pagenum2), keyword, new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				Caselist caselist = (Caselist) params[0];
//				if(caselist != null){
//					List<CaseDetailEntity> case_list = caselist.getCase_list();
//					SharePreferenceHelper.saveUncloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
//					uncloselist.clear();
//					for(int i=0;i<case_list.size();i++){
//						uncloselist.add(case_list.get(i));
//					}
//					adapter.notifyDataSetChanged();
//				}
				if(caselist != null){
					List<CaseDetailEntity> case_list = caselist.getCase_list();
					if(pagenum2 == 1){
						uncloselist.clear();
					}
					if (case_list != null && case_list.size() == 0) {
						pagenum2--;
						ToastHelper.showMsg(HomeContactMyCaseGroupActivity.this, "已加载到最新", false);
						return;
					}
					SharePreferenceHelper.saveUncloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
					for(int i=0;i<case_list.size();i++){
						uncloselist.add(case_list.get(i));
					}
					adapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onFailed(String Message) {
				Toast.makeText(HomeContactMyCaseGroupActivity.this, Message, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public void getMyClosecase(){
		if(caseservice == null){
			caseservice = new CaseService(HomeContactMyCaseGroupActivity.this);
		}
		caseservice.getClsecase(id, String.valueOf(pagenum1), new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				Caselist caselist = (Caselist) params[0];
//				if(caselist != null){
//					List<CaseDetailEntity> case_list = caselist.getCase_list();
//					SharePreferenceHelper.saveCloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
//					closelist.clear();
//					for(int i=0;i<case_list.size();i++){
//						closelist.add(case_list.get(i));
//					}
//					adapter.notifyDataSetChanged();
//				}
				if(caselist != null){
					List<CaseDetailEntity> case_list = caselist.getCase_list();
					if(pagenum1 == 1){
						closelist.clear();
					}
					if (case_list != null && case_list.size() == 0) {
						pagenum1--;
						ToastHelper.showMsg(HomeContactMyCaseGroupActivity.this, "已加载到最新", false);
						return;
					}
					SharePreferenceHelper.saveCloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
					for(int i=0;i<case_list.size();i++){
						closelist.add(case_list.get(i));
					}
					adapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onFailed(String Message) {
				Toast.makeText(HomeContactMyCaseGroupActivity.this, Message, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public void getSearchMyClosecase(){
		if(caseservice == null){
			caseservice = new CaseService(HomeContactMyCaseGroupActivity.this);
		}
		caseservice.getSearchClsecase(id, String.valueOf(pagenum3), keyword, new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				Caselist caselist = (Caselist) params[0];
//				if(caselist != null){
//					List<CaseDetailEntity> case_list = caselist.getCase_list();
//					SharePreferenceHelper.saveCloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
//					closelist.clear();
//					for(int i=0;i<case_list.size();i++){
//						closelist.add(case_list.get(i));
//					}
//					adapter.notifyDataSetChanged();
//				}
				if(caselist != null){
					List<CaseDetailEntity> case_list = caselist.getCase_list();
					if(pagenum3 == 1){
						closelist.clear();
					}
					if (case_list != null && case_list.size() == 0) {
						pagenum3--;
						ToastHelper.showMsg(HomeContactMyCaseGroupActivity.this, "已加载到最新", false);
						return;
					}
					SharePreferenceHelper.saveCloseCaseList(HomeContactMyCaseGroupActivity.this, id, case_list);
					for(int i=0;i<case_list.size();i++){
						closelist.add(case_list.get(i));
					}
					adapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onFailed(String Message) {
				Toast.makeText(HomeContactMyCaseGroupActivity.this, Message, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	// 上拉加载更多异步任务加载
			public class GetRefreshData extends AsyncTask<Void, Void, String[]> {

				@Override
				protected String[] doInBackground(Void... params) {
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return null;
				}

				@Override
				protected void onPostExecute(String[] result) {
					if(i == 0 && isdianji == true){
						pagenum++;
						getMyUnclosecase();
					}else if(i == 1 && isdianji == true){
						pagenum1++;
						getMyClosecase();
					}else if(i == 0 && isdianji == false){
						pagenum2++;
						getSearchMyUnclosecase();
					}else if(i == 1 && isdianji == false){
						pagenum3++;
						getSearchMyClosecase();
					}
					pullToRefreshScrollView.onRefreshComplete();
					super.onPostExecute(result);
				}
			}
	
}
